# 第一问 用for循环和while来完成简单的计数

# 采用for循环 计算 1+2+3+...+100的总和
def for_use():
    sum = 0
    for i in range(1, 101):
        sum += i
    return sum


# 采用while循环 计算 1+2+3+...+100的总和
def while_use():
    sum = 0
    num = 1
    while num < 101:
        sum += num
        num += 1
    return sum


# 第二问 用for循环和while循环两种方式来实现斐波那契函数, 限制在100以内

# for循环实现斐波那契函数
def fib_for():
    # f(1)
    a = 1
    # f(2)
    b = 1
    # f(0)
    c = 0
    print(c)
    print(a)
    print(b)
    for i in range(3, 101):
        # f(n) = f(n-1) + f(n-2)
        c = a + b
        # 交换变量
        a = b
        b = c
        print(c)


# while循环实现斐波那契函数
def fib_while():
    # f(1)
    a = 1
    # f(2)
    b = 1
    # f(0)
    c = 0
    i = 0
    print(c)
    print(a)
    print(b)
    while i <= 100:
        # f(n) = f(n-1) + f(n-2)
        c = a + b
        # 交换变量
        a = b
        b = c
        print(c)
        i += 1


# 第三问 自定义异常ParamsError
class ParamsError(Exception):
    pass


def add(a, b):
    return a + b


def sub(a, b):
    return a - b


# 乘法
def mul(a, b):
    return a * b


# 除法
def divide(a, b):
    try:
        return a / b
    except ZeroDivisionError:
        raise ParamsError("分母不可以为0")


# 整除
def edivide(a, b):
    try:
        return a // b
    except ZeroDivisionError:
        raise ParamsError("分母不可以为0")



# 取余
def getremainder(a, b):
    try:
        return a % b
    except ZeroDivisionError:
        raise ParamsError("分母不可以为0")


def sqrt(a, b):
    return a ** (1/b)
